package com.mini.repository;

import com.mini.model.BusinessTeacherWechatInfo;
import com.mini.model.BusinessTeacherWechatInfoKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author zhangxiaobin
 * @title: BusinessTeacherWechatInfoRepository
 * @description: TODO
 * @date 2020/3/2315:13
 */
@Repository
public interface BusinessTeacherWechatInfoRepository  extends JpaRepository<BusinessTeacherWechatInfo, BusinessTeacherWechatInfoKey>, JpaSpecificationExecutor<BusinessTeacherWechatInfo> {

    Long countByWxNoAndTeacherIdNot(String wxNo,Long teacherId);

    Long countByCorpUserIdAndTeacherIdNot(String corpId,Long teacherId);

    Long countByWxNoAndTeacherId(String wxNo,Long teacherId);

    Long countByTeacherId(Long teacherId);

    Long countByTeacherIdAndStates(Long teacherId,Integer states);

    BusinessTeacherWechatInfo findByTeacherIdAndWxNo(Long teacherId,String wxNo);

    BusinessTeacherWechatInfo findByWxNo(String wxNo);

    List<BusinessTeacherWechatInfo> findByTeacherIdOrderByOrdersAsc(Long teacherId);

    List<BusinessTeacherWechatInfo> findByTeacherIdAndUseTypeOrderByOrdersAsc(Long teacherId,Integer useType);
    List<BusinessTeacherWechatInfo> findByTeacherIdAndUseTypeAndStatesOrderByOrdersAsc(Long teacherId,Integer useType,Integer states);


    @Modifying
    @Query(value = "update business_teacher_wechat_info set states = ?3 where teacher_id = ?1 and wx_no =?2 ",nativeQuery = true)
    void updateWechatStates(Long teacherId, String wxNo,Integer states);

    @Query(value = "SELECT max(w.orders) from business_teacher_wechat_info w where w.teacher_id = ?1 ",nativeQuery = true)
    Long findMaxOrdersByTeacherId(Long teacherId);

    @Modifying
    @Transactional(rollbackOn=Exception.class)
    void deleteByTeacherId(Long teacherId);

    @Modifying
    @Transactional(rollbackOn=Exception.class)
    void deleteByTeacherIdAndWxNoIn(Long teacherId,List<String> wxNo);

    BusinessTeacherWechatInfo findByTeacherIdAndUseType(Long teacherId,Integer useType);

    BusinessTeacherWechatInfo findByTeacherIdAndCorpUserId(Long teacherId,String corpUserId);

    List<BusinessTeacherWechatInfo> findByTeacherIdAndUseTypeInOrderByOrdersAsc(Long teacherId,List<Integer> useTypes);

    List<BusinessTeacherWechatInfo> findByTeacherIdInAndStates(List<Long> teacherIds , Integer state);

}
